<script type="text/javascript">
	$(document).ready(function(){

		$('#petition').dataTable();
		
	});

	function verifyPetition(pet_id){
		var task = "verify";
			$.post('process_status_welfare.php',{task: task, pet_id: pet_id}, function(data) {
					window.location.href="index.php?pages=verify_approved_welfare";
			});

	}

	function cancelPetition(pet_id){
		var task = "cancel";
		//alert(id);

		$('<div>').dialog2({
			autoOpen: true,
			title: "ยกเลิกฎีกา",
			content: "dlg_cancel_welfare.php?pet_id=" + pet_id,
			id: "modal-cancel"
		});

	}

	function approvedPetition(pet_id){
		var task = "approved";

		$('<div>').dialog2({
			autoOpen: true,
			title: "อนุมัติ",
			content: "dlg_date_approved_welfare.php?pet_id=" + pet_id + "&task=" + task,
			id: "modal-date"
		});


	}
	function disapprovalPetition(pet_id){
		var task = "disapproval";

			$.post('process_status_welfare.php',{task: task, pet_id: pet_id}, function(data) {
				window.location.href="index.php?pages=verify_approved_welfare";
			});

	}

</script>

<?php
	$sql = "SELECT  tbl_welfare.pet_id, expenses1_name, expenses2_name, expenses3_name, panngan2_name, panngan3_name, sum(welfare) as welfare, verify_status, approved_status FROM tbl_welfare, expenses_lvl1, expenses_lvl2, expenses_lvl3, panngan2, panngan3 WHERE lvl1_id = '" .$_SESSION["lvl1_id"]. "' and expenses_lvl1.expenses1_id = ex1_id and expenses_lvl2.expenses2_id = ex2_id and expenses_lvl3.expenses3_id = ex3_id and panngan2.panngan2_id = tbl_welfare.panngan2_id and panngan3.panngan3_id = tbl_welfare.panngan3_id
	GROUP BY tbl_welfare.pet_id, expenses1_name, expenses2_name, expenses3_name, panngan2_name, panngan3_name, verify_status, approved_status
	";
	$db = new myDBS;
	$db->selectDB("account");
	$result = $db->runSQL($sql);

?>

<table id="petition" class="table table-striped" width="100%" >
	<caption><br></caption>
	<thead>
		<tr>
			<th>#</th>
			<th>เลขที่ฎีกา</th>
			<th>แผนงาน</th>
			<th>งาน</th>
			<th>ประเภท</th>
			<th>รายการ</th>
			<th>จำนวนเงิน</th>
			<th>ตรวจรับฎีกา</th>
			<th>อนุมัติ</th>
		</tr>
	</thead>
	<tbody>
	<?php
		$i=0;
		while ( $rows = $db->fetch_object ( $result )){
			$i++;
			echo "<tr>
					<td>$i</td>
					<td>$rows->pet_id</td>
					<td>$rows->panngan2_name</td>
					<td>$rows->panngan3_name</td>
					<td>$rows->expenses2_name</td>
					<td>$rows->expenses3_name</td>
					<td>". number_format($rows->welfare, 2)."</td>";
			echo "<td>";
			if($rows->verify_status == "รอตรวจสอบ") {
				
				echo "<a href='javascript:verifyPetition(\"$rows->pet_id\")' data-toggle='tooltip' title='ผ่าน'><i class='icon-ok'></i></a>&nbsp;<big>|</big>&nbsp;<a href='javascript:cancelPetition(\"$rows->pet_id\")' data-toggle='tooltip' title='ไม่ผ่าน'><i class='icon-remove'></i></a>";
				
			} else {
				echo $rows->verify_status;
			}
			echo "</td>";
			echo "<td>	 ";
			if($rows->verify_status == "ผ่าน") {
				if($rows->approved_status == "รออนุมัติ") {
					echo "<a href='javascript:approvedPetition(\"$rows->pet_id\")' data-toggle='tooltip' title='อนุมัติ'><i class='icon-ok'></i></a>&nbsp;<big>|</big>&nbsp;<a href='javascript:disapprovalPetition(\"$rows->pet_id\")' data-toggle='tooltip' title='ไม่อนุมัติ'><i class='icon-remove'></i></a>";
					
				} else {
					echo $rows->approved_status;
				}
			} else {
				echo "รอการตรวจสอบ";
			}
			echo "</td>";
			echo "</tr>";

		}
	?>
	</tbody>
</table>